package ca.camosun.morsecode;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;

import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.Toast;

public class ManualMorseActivity extends Activity {

	public enum Symbol {

		DOT(".", 300), DASH("-", DOT.timeInMilliseconds() * 3), TOKEN_PAUSE(
				"#", DOT.timeInMilliseconds()), LETER_PAUSE("", DOT
				.timeInMilliseconds() * 3), LONG_PAUSE(" ", DOT
				.timeInMilliseconds() * 7);

		private final String signal;
		private final int timeInMillisec;

		private Symbol(String sig, int time) {
			this.signal = sig;
			this.timeInMillisec = time;
		}

		private String signal() {
			return signal;
		}

		private int timeInMilliseconds() {
			return timeInMillisec;
		}

	}

	/**
	 * Variable setup and class initialization.
	 */
	private int timePressed = 0;
	private int timeReleased = 0;
	Timer tapTimer = new Timer();
	MorseQueue toDecode = new MorseQueue();
	EditText readableMessage;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manual_morse);

		readableMessage = (EditText) findViewById(R.id.decoded_text);
		Intent textIntent = getIntent();
		readableMessage.setText(textIntent.getCharSequenceExtra("text"));
		readableMessage.setSelection(readableMessage.getText().length());
	}

	/**
	 * onTouchEvent() - Detects when the touch screen has been touched. Alex:
	 * Added if statements to start producing an output string.
	 */
	@Override
	public boolean onTouchEvent(MotionEvent touch) {

		TimerTask pressedTimer = new IncrementPressedTimer();
		TimerTask ReleasedTimer = new IncrementReleasedTimer();

		switch (touch.getAction()) {
		case MotionEvent.ACTION_DOWN:

			// Since the screen has been /pressed/ right now...

			// catch the edge case where this is the first button press

			if (timeReleased == 0) {
				tapTimer.scheduleAtFixedRate(pressedTimer, 0, 10);
				return true;

			}
			// in this case we want to add a new token, NOT a new letter
			// so figure out which token to add based on the pressed timer


			if (timeReleased <= ManualMorseActivity.Symbol.TOKEN_PAUSE.timeInMilliseconds()) {

				if (timePressed <= ManualMorseActivity.Symbol.DOT.timeInMilliseconds()) {
					toDecode.addToken(MorseDecoder.Token.DOT);

					if (timePressed > ManualMorseActivity.Symbol.DOT.timeInMilliseconds()) {

						toDecode.addToken(MorseDecoder.Token.DASH); 
					}

				}
			}
			
			// in this case, the pause was not for at token, but for a letter or word,
			// so which was it?

			// we must have gotten a short pause, so add it to the queue 
			if (timeReleased > ManualMorseActivity.Symbol.TOKEN_PAUSE.timeInMilliseconds()) {
				if(timeReleased<= ManualMorseActivity.Symbol.LETER_PAUSE.timeInMilliseconds()){
					toDecode.addToken(MorseDecoder.Token.SHORT_PAUSE);
				}
				
				// we must have a log pause, so add it to the queue, and call OutputMessage
				if(timeReleased >ManualMorseActivity.Symbol.LETER_PAUSE.timeInMilliseconds()){
				
					toDecode.addToken(MorseDecoder.Token.LONG_PAUSE); 
					outputMessage(); 
				}
				
			}

			timeReleased = 0;
			// we have to do this last because we needed the length of the last press to know what to add to the queue
			tapTimer.scheduleAtFixedRate(pressedTimer, 0, 10);
		case MotionEvent.ACTION_UP:
			tapTimer.scheduleAtFixedRate(ReleasedTimer, 0, 10);


			timePressed = 0;

		}
		return true;
	}

	void outputMessage() {
		String decoded = "";

		while (toDecode.hasNext()) {
			decoded += toDecode.getNextDecoded();
		}

		if (decoded.contains(MorseDecoder.BAD_MORSE)) {
			decoded = decoded.replace(MorseDecoder.BAD_MORSE, "");

		}

		readableMessage.append(decoded);
		readableMessage.setSelection(readableMessage.getText().length());
	}

	class IncrementPressedTimer extends TimerTask {

		@Override
		public void run() {
			timePressed += 10;

		}

	}

	class IncrementReleasedTimer extends TimerTask {

		@Override
		public void run() {
			timeReleased += 10;

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.activity_manual_morse, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent inputModeIntent = null;

		switch (item.getItemId()) {

		case R.id.easy_morse:
			inputModeIntent = new Intent(this, MorseButtonPadActivity.class);
			int start = 0;
			int finish = 1;
			inputModeIntent.putExtra("text", readableMessage.getText());
			ActivityOptions options = ActivityOptions.makeScaleUpAnimation(
					readableMessage, start, start, finish, finish);
			finish();
			startActivity(inputModeIntent, options.toBundle());
			return true;

		}
		return false;
	}

}
